package com.um.psystem.controller.exManage;

import com.um.psystem.controller.BaseController;
import com.um.psystem.entity.sysEntity.DeptEntity;
import com.um.psystem.model.vo.DataGrid;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.exService.IExceptDictService;
import com.um.psystem.service.exService.IExceptEventService;
import com.um.psystem.service.exService.impl.ExceptDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: AiLen2E
 * @Date: 2021/11/1 10:19
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "/exception")
public class ExceptionManageController extends BaseController {

    @Autowired
    IExceptDictService exceptDictService;
    @Autowired
    IExceptEventService exceptEventService;

    @RequestMapping(value = "/getDeptList")
    @ResponseBody
    public DataGrid getDeptList(@RequestParam Map map) {
        List<Map<String, Object>> assetsTypeList = exceptDictService.getDeptList(map);
        int count = 0;
        if (assetsTypeList != null && assetsTypeList.size() > 0)
            count = Integer.parseInt(assetsTypeList.get(0).get("totals").toString());
        return buildDataGrid(assetsTypeList, count);
    }

    @RequestMapping(value = "/deleDept", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult deleDept(@RequestParam Map map) {

        if(map.get("id")==null || map.get("id").toString().equals(""))
        { return JsonResult.error(403,"非法请求"); }
        String idStr = map.get("id").toString();
        Integer id = Integer.parseInt(idStr);
        return  exceptDictService.deleDept(id);
    }

    @RequestMapping(value = "/addDept", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addDept(@RequestParam Map map) {
        return exceptDictService.addDept(map);
    }

    @RequestMapping(value = "/editDept", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editDept(@RequestParam Map map) {
        return exceptDictService.editDept(map);
    }

    @RequestMapping(value = "/getExcTypeList")
    @ResponseBody
    public DataGrid getExceptionTypeList(@RequestParam Map map) {
        List<Map<String, Object>> assetsTypeList = exceptDictService.getExceptionTypeList(map);
        int count = 0;
        if (assetsTypeList != null && assetsTypeList.size() > 0)
            count = Integer.parseInt(assetsTypeList.get(0).get("totals").toString());
        return buildDataGrid(assetsTypeList, count);
    }

    @RequestMapping(value = "/deleExcType", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult deleExceptionType(@RequestParam Map map) {

        if(map.get("id")==null || map.get("id").toString().equals(""))
        { return JsonResult.error(403,"非法请求"); }
        String idStr = map.get("id").toString();
        Integer id = Integer.parseInt(idStr);
        return  exceptDictService.deleExceptionType(id);
    }

    @RequestMapping(value = "/addExcType", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addExceptionType(@RequestParam Map map) {
        return exceptDictService.addExceptionType(map);
    }

    @RequestMapping(value = "/editExcType", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editExceptionType(@RequestParam Map map) {
        return exceptDictService.editExceptionType(map);
    }

    @RequestMapping(value = "/getEventList")
    @ResponseBody
    public DataGrid getExceptionEventList(@RequestParam Map map) {
        List<Map<String, Object>> assetsTypeList = exceptEventService.getExceptionEventList(map);
        int count = 0;
        if (assetsTypeList != null && assetsTypeList.size() > 0)
            count = Integer.parseInt(assetsTypeList.get(0).get("totals").toString());
        return buildDataGrid(assetsTypeList, count);
    }

    @RequestMapping(value = "/deleEvent", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult deleExceptionEvent(@RequestParam Map map) {

        if(map.get("eid")==null || map.get("eid").toString().equals(""))
        { return JsonResult.error(403,"非法请求"); }
        String eidStr = map.get("eid").toString();
        Integer eid = Integer.parseInt(eidStr);
        return  exceptEventService.deleExceptionEvent(eid);
    }

    @RequestMapping(value = "/addEvent", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addExceptionEvent(@RequestParam Map map) {
        return exceptEventService.addExceptionEvent(map);
    }

    @RequestMapping(value = "/editEvent", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editExceptionEvent(@RequestParam Map map) {
        return exceptEventService.editExceptionEvent(map);
    }

    @RequestMapping(value = "/list_dept", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getDept() {

        List<Map<String, Object>> list = exceptDictService.getDeptList(new HashMap());

        return JsonResult.success(list);
    }

    @RequestMapping(value = "/list_exception_type", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getExceptionType() {

        List<Map<String, Object>> list = exceptDictService.getExceptionTypeList(new HashMap());

        return JsonResult.success(list);
    }
//------------------------------------------------------------

    @RequestMapping(value = "/getExceptionDtlList")
    @ResponseBody
    public DataGrid getExceptionDtlList(@RequestParam Map map) {
        List<Map<String, Object>> assetsTypeList = exceptEventService.getExceptionEventDtlList(map);
        int count = 0;
        if (assetsTypeList != null && assetsTypeList.size() > 0)
            count = Integer.parseInt(assetsTypeList.get(0).get("totals").toString());
        return buildDataGrid(assetsTypeList, count);
    }

    @RequestMapping(value = "/deleExceptionDtl", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult deleExceptionDtl(@RequestParam Map map) {

        if(map.get("id")==null || map.get("id").toString().equals(""))
        { return JsonResult.error(403,"非法请求"); }
        String idStr = map.get("id").toString();
        Integer id = Integer.parseInt(idStr);
        return  exceptEventService.deleExceptionEventDtl(id);
    }

    @RequestMapping(value = "/addExceptionDtl", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addExceptionDtl(@RequestParam Map map) {
        return exceptEventService.addExceptionEventDtl(map);
    }

    @RequestMapping(value = "/editExceptionDtl", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editExceptionDtl(@RequestParam Map map) {
        return exceptEventService.editExceptionEventDtl(map);
    }

    @RequestMapping(value = "/setEventStatusToFinish", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult setEventStatusToFinish(Integer eid)
    {return exceptEventService.setEventState(eid,2);}

    @RequestMapping(value = "/exportExceptionEventData",method = RequestMethod.GET)
    public void exportExceptionEventData(HttpServletResponse response, @RequestParam Map map)
    {
        System.out.println(map);
        for (Object key : map.keySet())
        {
            String str = map.get(key).toString();
            try {
                map.put(key,new String(str.getBytes("ISO-8859-1"),"UTF-8"));
            }catch (UnsupportedEncodingException e)
            {e.printStackTrace();}
        }
        exceptEventService.exportExceptionEventData(response,map);
    }

    @RequestMapping(value = "/getEtsd", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Map> getExceptionTypeStatisticalData(@RequestParam Map map) {
        return exceptEventService.getExceptionTypeStatisticalResult(map);
    }

    @RequestMapping(value = "/getDsd", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Map> getDeptStatisticalData(@RequestParam Map map) {
        return exceptEventService.getDeptStatisticalResult(map);
    }
}
